package cn.gailvlun.gll.presentation.hotel.day

import android.content.Context
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView


class ScaleTemplateLayoutManager : LinearLayoutManager {

    val minScale = 1f
    val context: Context

    constructor(context: Context?) : super(context) {
        this.context = context!!
    }

    override fun onLayoutCompleted(state: RecyclerView.State?) {
        super.onLayoutCompleted(state)

        scaleByPosition()
    }

    override fun scrollHorizontallyBy(dx: Int, recycler: RecyclerView.Recycler?, state: RecyclerView.State?): Int {
        scaleByPosition()
        return super.scrollHorizontallyBy(dx, recycler, state)
    }

    override fun getPaddingLeft(): Int {
        return (context.resources.displayMetrics.density * 36f).toInt()
    }

    override fun getPaddingRight(): Int {
        return paddingLeft
    }

    private fun scaleByPosition() {
        val mid = width / 2

        for (i in 0 until childCount) {
            getChildAt(i).apply {
                val childMid = (getDecoratedLeft(this) + getDecoratedRight(this)) / 2
                val distance2mid = Math.abs(childMid - mid)
                val scale = 1f - minScale * distance2mid / (mid * 12)
                this.scaleX = scale
                this.scaleY = scale
            }
        }
    }
}